// <copyright file="NavigationController.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Web.Controllers
{
    using System.Web.Mvc;
    using System;
    using System.Linq;
  
    using CleverCMS.Core.Extensions;
    using CleverCMS.Cms.Services;
    using CleverCMS.Cms.Data.Entities;

    public class NavigationController : Controller
    {
        private readonly MenuService        m_menuService;
        private readonly MenuItemService    m_menuItemService;
        private readonly PageService        m_pageService;

        public NavigationController(MenuService         menuService, 
                                    MenuItemService     menuItemService,
                                    PageService         pageService)
        {
            this.m_menuService = menuService;
            this.m_menuItemService = menuItemService;
            this.m_pageService = pageService;
        }

        public ActionResult Show(string menuName)
        {
            CmsMenu menu = m_menuService.LoadMenu(menuName);
            return PartialView(menuName, menu);
        }

        public ActionResult CreateMenuItems()
        {
            var model = m_menuService.LoadAllMenuItems().Where(x => x.Visible == true && x.MenuId == 1).OrderBy(x => x.SortOrder).ToList();
            return PartialView("_MenuControl", model);
        }

        #region MenuItems admining from page

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddMenuItem(int? menuParentId, string menuTitle, string pageContent)
        {
            try
            {
                if (!String.IsNullOrEmpty(menuTitle))
                {
                    var page = new CmsPage
                    {
                        PageTitle = menuTitle,
                        PageContent = pageContent,
                        CategoryId = 1
                    };

                    m_pageService.CreateEntry(page);
                    page.Alias = CreateAliasExtensions.CreateAlias(page.PageTitle, null);
                    page.StatusId = (int)StatusCode.Published;
                    m_pageService.Save(page);

                    var menuItem = new CmsMenuItem
                    {
                        ParentId = menuParentId,

                        NavigateUrl = Url.Action("Content","Home", new { area = "", name = page.Alias }),
                        Title = menuTitle,
                        MenuId = 1,
                        EntryId = null,
                        CreatedAt = DateTime.Now,
                        Visible = true,
                        SortOrder = 0
                    };

                    m_menuService.SaveMenuItem(menuItem);

                    return Json(new { success = true, message = "Page and menu item added!", pageLink = menuItem.NavigateUrl, menuId = menuItem.Id });              
                }
                else
                {
                    return Json(new { success = false, message = "Title is empty" });    
                }
            }
            catch (Exception)
            {
                return Json(new {success = false, message = "unknown error"});
            }    
        }

        #endregion
    }
}